package cn.com.connext.scrm.repository.master;

import cn.com.connext.scrm.domain.master.OrderRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface OrderRepository extends JpaRepository<OrderRecord, String>, JpaSpecificationExecutor {

    @Query(value = "select a.* from order_record a where a.user_id = :userId and a.product_id = :productId and a.state =0 order by create_time asc ", nativeQuery = true)
    List<OrderRecord> findByUserIdAndProductId(@Param("userId") String userId, @Param("productId") String productId);
    @Query(value = "select a.* from order_record a where a.user_id = :userId and a.order_name = :productName order by create_time asc ", nativeQuery = true)
    List<OrderRecord> findByUserIdAndProductName(@Param("userId") String userId, @Param("productName") String productName);

    OrderRecord findByOrderCode(@Param("orderCode") String orderCode);


    @Query(value = "SELECT * FROM order_record WHERE user_id = :userId  AND TYPE in (0,2) AND state = 0  order by create_time desc", nativeQuery = true)
    List<OrderRecord> findByUserId(@Param("userId")String userId);



    @Query(value = "SELECT * FROM order_record WHERE user_id = :userId  AND TYPE =1 AND state = 0  order by create_time desc ", nativeQuery = true)
    List<OrderRecord> findNoWechatOrderByUserId(@Param("userId")String userId);

    @Query(value = "(SELECT * FROM order_record WHERE sku_id is not null ", nativeQuery = true)
    List<OrderRecord> findOrderMsgBySkuId();

    /**
     * 根据用户ID查询记录
     *
     * @param userId
     * @return
     */
    List<OrderRecord> findOrderRecordsByUserId(String userId);

    @Query(value = "select a.* from order_record a where a.user_id = :userId and a.product_id = :productId and a.good_type is not null order by create_time desc ", nativeQuery = true)
    List<OrderRecord> findOrderOneByCarRoll(@Param("userId") String userId, @Param("productId") String productId);

    @Query(value = "select a.* from order_record a where a.user_id = :userId and a.product_id = :productId and a.prize_id = :prizeId and a.good_type is not null order by create_time desc ", nativeQuery = true)
    List<OrderRecord> findOrderOneByCarRollPrizeId(@Param("userId") String userId, @Param("productId") String productId, @Param("prizeId") Integer prizeId);

}
